<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * A base model with a series of CRUD functions (powered by CI's query builder)
 */
class MY_Model extends CI_Model{

	public $short_table;
	public $full_table;
	public $primary_key;

    /**
     * construct
     */
    public function __construct(){
        parent::__construct();
        $this->load->helper(array('form','url'));
    }

	/**
     * getAll
     * @param mixed $orderby, such as 'id desc'
     * @return mixed
     */
	public function getAll($orderby = ""){
		$sql = "select * from {$this->full_table}";
		if(empty($orderby) == false){
			$sql .= " order by $orderby";
		}
		$data = $this->db->query($sql)->result_array();
		return $data;
	}

	/**
     * update by sql
     */
	public function update_by_sql($sql){
		$this->db->query($sql);
	}

	/**
     * add
     * @param mixed $data
     */
	public function add( $data ){
		$this->db->insert($this->short_table, $data);
		return $this->db->insert_id();
	}

	/**
     * update
     * @param mixed $id
     * @param mixed $data
     */
	public function update_common($id, $data){
		$this->db->update( $this->short_table, $data, array( $this->primary_key => $id ) );
	}

	/**
     * update
     * @param mixed $id
     * @param mixed $data
     */
	public function update_common_by_col($colomn, $value, $data){
		$this->db->update( $this->short_table, $data, array( $colomn => $value ) );
		return $this->db->affected_rows();
	}

	/**
     * update by array
     * @param mixed $condition
     * @param mixed $data
     * @return mixed
     */
	public function update_common_by_array($condition, $data){
		$this->db->update( $this->short_table, $data, $condition );
		return $this->db->affected_rows();
	}

	/**
     * get by condition
     * @param mixed $columns such as 'id, thumb, title' or '*'
     * @param mixed $condition such as 'array('recipe_id' => 123)'
     * @param mixed $order_by such as 'array('id' => 'asc')'
     */
	public function getManyByCondition($columns, $condition, $order_by = null){
		if( $order_by == null ){
			$data = $this->db->select($columns)->get_where($this->short_table, $condition )->result_array();
		}
		else{
			$data = $this->db->select($columns)->where( $condition )->from($this->short_table)->order_by($order_by[0], $order_by[1])->get()->result_array();
		}
		return $data;
	}

	/**
     * get by primary key
     * @param mixed $primary_key the value of primary key
     * @param mixed $column
     */
	public function getByPrimaryKey($primary_key, $column = "*"){
		$data = $this->db->select($column)->get_where($this->short_table, array( $this->primary_key => $primary_key ))->result_array();
		return $data;
	}

	/**
     * delete by condition
     * @param mixed $condition array
     */
	public function deleteByCondition( $condition ){
		$this->db->where( $condition )->delete( $this->short_table );
	}

	/**
     * count num of data
     * @param mixed $condition array
     */
	public function getNumByCondition( $condition=NULL ){
		if(isset($condition)){
			$num = $this->db->where( $condition )->from($this->short_table)->count_all_results();
		}else{
            $num = $this->db->from($this->short_table)->count_all_results();
		}

		return $num;
	}
	/**
     * count num of data
     * @param mixed $condition array
     */
    public function getNumBySearch($key,$value){
        $num = $this->db->like($key,$value)->where(array('status'=>'1'))->from($this->short_table)->count_all_results();
        return $num;
    }

}